package dbTable;
import db_properties_path.*;
import java.sql.*;
import java.util.*;
import dbTool.*;


public class TOPIC extends DBconnection2 {
public TOPIC(SetPath set){
super(set);
}
public TOPIC(){
super(new DefaultSetPath());
}
/** Auto Increasment */
/** Primary Key */
/** INTEGER ; java.lang.Integer */private int topicid;
/** VARCHAR ; java.lang.String */private String topicname;
/** INTEGER ; java.lang.Integer */private int groupid;
/** INTEGER ; java.lang.Integer */private int accountid;
/** VARCHAR ; java.lang.String */private String content;
/** DATETIME ; java.sql.Timestamp */private long topictime;
public int getTopicid(){
return  topicid;
}
public void setTopicid(int topicid){
this.topicid= topicid;
}

public String getTopicname(){
return  topicname;
}
public void setTopicname(String topicname){
this.topicname= topicname;
}

public int getGroupid(){
return  groupid;
}
public void setGroupid(int groupid){
this.groupid= groupid;
}

public int getAccountid(){
return  accountid;
}
public void setAccountid(int accountid){
this.accountid= accountid;
}

public String getContent(){
return  content;
}
public void setContent(String content){
this.content= content;
}

public long getTopictime(){
return  topictime;
}
public void setTopictime(long topictime){
this.topictime= topictime;
}

public interface fn{
String TABLE_NAME = "TOPIC";
String PK = "topicid";
String topicid="topicid";
String topicname="topicname";
String groupid="groupid";
String accountid="accountid";
String content="content";
String topictime="topictime";
}
public String sql(String SQL){
return Utility.getSQL組合字(SQL, TOPIC.fn.class);
}
public int clear() {
String SQL = "delete from TOPIC";
Object parameter[] = {};
return super.excuteUpdate(SQL, parameter);
}

public int delete(int topicid) {
String SQL = "delete from TOPIC where topicid=?";
Object parameter[] = {topicid};
return super.excuteUpdate(SQL, parameter);
}

public int update(String topicname,int groupid,int accountid,String content,long topictime, int topicid){
String SQL = "update TOPIC set topicname=?,groupid=?,accountid=?,content=?,topictime=? where topicid=?";
Object parameter[] = {topicname,groupid,accountid,content,new Timestamp(topictime),topicid};
return super.excuteUpdate(SQL,parameter);
}

public int insert(String topicname,int groupid,int accountid,String content,long topictime){
String SQL = "insert into TOPIC(topicname,groupid,accountid,content,topictime) values(?,?,?,?,?)";
Object parameter[] = { topicname,groupid,accountid,content,new Timestamp(topictime)};
return super.excuteUpdate(SQL ,parameter);
}

public String[][] select() {
String SQL = "select * from TOPIC";
Object parameter[] = {};
return super.select(SQL, parameter);
}

public TOPIC[] select_Object() {
String SQL = "select * from TOPIC";
Object parameter[] = {};
String data[][] = super.select(SQL, parameter);TOPIC obj[] = toObject(data);return obj;}

public String[][] select( int topicid) {
String SQL = "select * from TOPIC where topicid=?";
Object parameter[] = {topicid};
return super.select(SQL, parameter);}

public TOPIC[] select_Object( int topicid) {
String data[][] = this.select(topicid);TOPIC obj[] = toObject(data);return obj;}

public int selectCount() {
String SQL = "select count(topicid) from TOPIC";
Object parameter[] = {};
String d[][] = super.select(SQL, parameter);
return Integer.parseInt(d[0][0]);}

public String[][] selectPage(int pageSize, int pageIndex, int rowCount) {
int desc = PaggingSQL.ASC;PaggingSQL ps = new dbTool.MySQL();String TABLE_NAME = fn.TABLE_NAME;String PK = fn.PK;String SQL = ps.getSelectPage(pageSize, pageIndex, TABLE_NAME, PK, desc, rowCount);Object parameter[] = {};
return super.select(SQL, parameter);
}
public String[][] selectPageOrderBy(int pageSize, int pageIndex, String sortName, int desc, int rowCount) {
PaggingSQL ps = new dbTool.MySQL();String TABLE_NAME = fn.TABLE_NAME;String PK = fn.PK;String SQL = ps.getSelectPageOrderBy(pageSize, pageIndex, TABLE_NAME, PK, sortName, desc, rowCount);Object parameter[] = {};
return super.select(SQL, parameter);
}
 /**
 * 關鍵字 where 不用寫
 * @param pageSize
 * @param pageIndex
 * @param sortName
 * @param desc
 * @param rowCount
 * @param whereCondition = 【pk>0】, 關鍵字 where 不用寫
 * @return
 */
public String[][] selectPageOrderBy(int pageSize, int pageIndex, String sortName, int desc, int rowCount, String whereCondition) {
PaggingSQL ps = new dbTool.MySQL();String TABLE_NAME = fn.TABLE_NAME;String PK = fn.PK;
//範例,關鍵字 where 不用寫, ex:【pk>0】
String SQL = ps.getSelectPageOrderBy(pageSize, pageIndex, TABLE_NAME, PK, sortName, desc, rowCount, whereCondition);Object parameter[] = {};
return super.select(SQL, parameter);
}
public TOPIC[] selectPage_Object(int pageSize, int pageIndex, int rowCount) {
	String data[][] = this.selectPage(pageSize, pageIndex, rowCount);
	TOPIC obj[] = toObject(data);
	return obj;
}
 /**
 * 分組統計筆數
 * @param fk_List
 * @return
 */
public Map<Integer, Integer> getGroupCount(String fkName, LinkedList<Integer> fk_List){String q = "";
Object parameter[] = new Object[fk_List.size()];
int i=0;
for (Integer no : fk_List) {
q += "?,";
parameter[i++] = no;
}
if (q.length() > 0){
q = q.substring(0, q.length()-1);
}
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();String SQL = "";SQL += "SELECT "+ fkName +", count("+ fkName +") ";SQL += "FROM "+ fn.TABLE_NAME +" where "+fkName+" in (" + q + ")";SQL += "group by "+ fkName +";";String data[][] = super.select(SQL, parameter);for (int j = 0; j < data.length; j++) {int key = Integer.parseInt(data[j][0]);int count = Integer.parseInt(data[j][1]);map.put(key, count);}return map;} /**
 * Where PK in(?,?,?....)
 * @param idList
 * @return
 */
public Map<Integer, TOPIC> selectMap(LinkedList<Integer> idList){
String q = "";
Object parameter[] = new Object[idList.size()];
int i=0;
for (Integer no : idList) {
q += "?,";
parameter[i++] = no;
}
if (q.length() > 0){
q = q.substring(0, q.length()-1);
}
HashMap<Integer, TOPIC> map = new HashMap<Integer, TOPIC>();

String SQL = "select * from TOPIC where topicid in ("+ q +")";
String data[][] = super.select(SQL, parameter);
TOPIC[] obj = this.toObject(data);
for (TOPIC o : obj) {
map.put(new Integer(o.topicid) , o);
}
return map;
}
public TOPIC[] toObject(String data[][]){
TOPIC[] obj = new TOPIC[data.length];
for (int i = 0; i < obj.length; i++) {
obj[i] = new TOPIC(super.getSetting());obj[i].topicid=Integer.parseInt(data[i][0]);
obj[i].topicname=data[i][1];
obj[i].groupid=Integer.parseInt(data[i][2]);
obj[i].accountid=Integer.parseInt(data[i][3]);
obj[i].content=data[i][4];
obj[i].topictime=Long.parseLong(data[i][5]);
}
return obj;}
}